<template>
  <div class="copyBom" @click.stop="aotuTab = false">
    <el-tabs v-model="activeName" type="border-card">
      <el-tab-pane :label="$t('market.bomInfo')" name="product">
        <div class="tool">
          <el-button size="mini" type="primary" @click="preservation">
            {{ $t('btn.save') }}<!--保存-->
          </el-button>
          <div v-if="bomCost('bom:price')" class="summation">
            {{ $t('market.materialcost') }}<!--物料成本-->:
            <span class="color-text">
              {{ Math.round((materialScience + Number.EPSILON) * 100) / 100 }}{{ $t('market.yuan') }}元
            </span>
            <span style="margin-right: 5px">{{ $t('market.documentstatus') }}<!--审核状态--></span>
            <el-tag type="primary">{{ FDOCUMENTSTATUS }}</el-tag>
          </div>
        </div>
        <div class="form-layout">
          <jc-form
            :option-value="prodValue"
            :options="prodOptions"
            label-width="100px"
            @handleMagnifier="handleMagnifier"
            @openNumber="getMaterielList"
            @mater="handleSonDetailMater(prodValue.FNUMBER)"
          />
        </div>
        <div style="margin-bottom: 5px;">
          <el-button size="mini" @click="newHang">
            {{ $t('btn.newHang') }}<!--新增行-->
          </el-button>
          <el-button size="mini" @click="delHang">
            {{ $t('btn.delHang') }}<!--删除行-->
          </el-button>
        </div>
        <jc-table
          :table-data="sonTableData"
          serial
          :table-header="sonTableHeader"
          get-row-keys="FMATERIALID"
          class="materiel-table"
          :table-height="tableHeight"
          @rowClick="rowClick"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item :label="$t('market.issueType')">
                  <el-select
                    v-model="props.row.FISSUETYPE"
                    size="mini"
                    @input="value => handleIssuing(scope.$index, value)"
                  >
                    <el-option
                      v-for="item in issueMaterialsOptions"
                      :key="item.FVALUE"
                      :label="item.FISSUETYPE"
                      :value="item.FVALUE"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item v-if="bomCost('bom:price')" :label="$t('market.billamount')">
                  <span>
                    {{
                      parseInt(props.row.FPRICE * props.row.FDOSAGE * 100) / 100
                    }}
                  </span>
                </el-form-item>
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteBom(props.$index)"
                >
                  {{ $t('btn.del') }}
                </el-button>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('market.materImg')"
            prop="FIMG"
            align="center"
            width="80px"
          >
            <template slot-scope="scope">
              <el-image
                :src="$img + scope.row.FIMG"
                class="materielImg"
                :preview-src-list="srcList"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('market.fmodel')"
            prop="FMODEL"
            align="center"
            width="130px"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            :label="$t('market.fmaterial')"
            prop="FNUMBER"
            align="center"
            width="170px"
          >
            <template slot-scope="scope">
              <el-input
                v-model.trim="scope.row.FNUMBER"
                size="mini"
                @input="getAtuo(scope.$index, scope.row.FNUMBER)"
              >
                <i
                  slot="prefix"
                  class="iconfont icon-jin-rud-ao-bo"
                  @click="handleSonDetailMater(scope.row.FNUMBER)"
                />
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="handleGetMaterialSon(scope.row, scope.$index)"
                />
              </el-input>
              <div
                v-if="aotuTab && tableIndex === scope.$index"
                :class="tableIndex < 7 ? 'tabPosit' : 'tabPosit1'"
              >
                <number-code
                  :bom-code="bomCode"
                  :fnumber="fnumber1"
                  @material="sonSelectRow"
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('shop.fname2')"
            prop="fname"
            align="center"
            min-width="130px"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            :label="$t('market.fdescripTion')"
            prop="FDESCRIPTION"
            align="center"
            min-width="200px"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            :label="$t('shop.fspecificaTion')"
            prop="FSPECIFICATION"
            align="center"
            min-width="100px"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            :label="$t('market.dosage')"
            prop="FDOSAGE"
            align="center"
          >
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.FDOSAGE"
                :precision="4"
                :step="0.0001"
                :min="0.0"
                size="mini"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('market.funit')"
            prop="FUNITNAME"
            align="center"
            width="80px"
          />
          <el-table-column
            :label="$t('market.fprice')"
            prop="FPRICE"
            align="center"
            max-width="80px"
          >
            <template slot-scope="scope">
              <el-input-number
                v-model="scope.row.FPRICE"
                :precision="4"
                :step="0.0001"
                :min="0.0"
                size="mini"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('market.isskip')"
            prop="FISSKIP"
            align="center"
            width="60px"
          >
            <template slot-scope="scope">
              <el-checkbox v-model="scope.row.FISSKIP" />
            </template>
          </el-table-column>
        </jc-table>
      </el-tab-pane>
      <el-tab-pane :label="$t('market.other')" name="other">
        <jc-form :option-value="otherUrlObject" :options="otherOptions" />
        <jc-table
          :table-header="logTableHeader"
          :table-data="otherLogTableData"
          serial
          table-height="33.76vh"
        />
        <jc-pagination
          v-show="otherPagination.total > 0"
          :total="otherPagination.total"
          :page.sync="otherPagination.pageNum"
          :limit.sync="otherPagination.pageSize"
          @pagination="handleOther"
        />
      </el-tab-pane>
    </el-tabs>
    <!--    父项列表弹窗-->
    <el-dialog
      v-dialogDrag
      class="dialogClass"
      :title="$t('menu.materielList')"
      model
      :visible.sync="parentTableVisible"
      :close-on-click-modal="false"
      width="80%"
      destroy-on-close
      @close="closeFun"
    >
      <el-slider v-model="scrollValue" @change="enlargeImg" />
      <!--      条件区域-->
      <div class="dialogForm">
        <div v-if="isTable === 0" class="bom-form">
          <search
            ref="inputValue"
            :options="selectData"
            :msg="tacitly"
            @seek="collect"
            @hand="queryParentSearch"
          />
          <el-button
            type="primary"
            size="mini"
            class="search-dialog btn"
            @click="queryParentSearch"
          >
            {{ $t('btn.search') }}
          </el-button>
        </div>
        <div v-else class="bom-form">
          <search
            ref="sonInputValue"
            :options="selectData"
            :msg="tacitly"
            @seek="collect"
            @hand="querySonSearch"
          />
          <el-button
            type="primary"
            size="mini"
            class="search-dialog btn"
            @click="querySonSearch"
          >
            {{ $t('btn.search') }}
          </el-button>
        </div>
      </div>
      <!--      父表格区域-->
      <div v-if="isTable === 0">
        <jc-table
          serial
          :table-data="parentTableData"
          table-height="62.8vh"
          :table-header="parentTableHeader"
          @clickRow="parentSelectRow"
        >
          <el-table-column
            :label="$t('market.materImg')"
            prop="FIMG"
            align="center"
            min-width="60px"
          >
            <template slot-scope="scope">
              <el-image
                :src="$img + scope.row.FIMG"
                class="materielImg"
                :preview-src-list="srcList"
              />
            </template>
          </el-table-column>
        </jc-table>
        <jc-pagination
          :total="parentPagination.total"
          :page.sync="parentPagination.pageNum"
          :limit.sync="parentPagination.size"
          @pagination="getMaterielList"
        />
      </div>
      <!--      子表格区域-->
      <div v-else>
        <jc-table
          serial
          :table-data="sonDialogTableData"
          table-height="62.8vh"
          :table-header="sonDialogHeaderTable"
          @clickRow="sonSelectRow"
        >
          <el-table-column :label="$t('market.materImg')" min-width="60px" align="center">
            <template slot-scope="scope">
              <el-image
                :src="$img + scope.row.fimg"
                class="materielImg"
                :preview-src-list="srcList"
              />
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('market.fmodel')"
            prop="fmodel"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column :label="$t('market.fmaterial')" align="center" width="150px">
            <template slot-scope="scope">
              {{ scope.row.fnumber }}
            </template>
          </el-table-column>
          <!--审核状态-->
          <template v-slot:btnState="col">
            <el-tag :type="col.scope.row.stateColor">{{ col.scope.row.fdocumentStatus }}</el-tag>
          </template>
          <!--禁用状态-->
          <template v-slot:tagSlot="clo">
            <el-tag v-if="clo.scope.row.fforbidStatus === '否'">
              {{ clo.scope.row.fforbidStatus }}
            </el-tag>
            <el-tag v-else type="danger">
              {{ clo.scope.row.fforbidStatus }}
            </el-tag>
          </template>
        </jc-table>
        <jc-pagination
          :total="sonPagination.total"
          :page.sync="sonPagination.pageNum"
          :limit.sync="sonPagination.size"
          @pagination="btnSearch"
        />
      </div>
    </el-dialog>
    <!--工艺模板-->
    <craft v-if="craftVis" @client="craftData" />
  </div>
</template>

<script>
import jcForm from '@/components/Form'
import getForm from '../createBom/components/getForm'
import {
  queryBomTreeDataList,
  queryMaterialfather,
  queryViewItem,
  queryMaterialSon,
  insertBom
} from '@/api/engineering/createBom'
import { uuid } from 'vue-uuid'
import { queryTBdMaterialList } from '@/api/basicManagement/materielList'
import { toMxAmina, Disable } from '@/components/ToMxamineState'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { queryBomchildList } from '@/api/engineering/editBom'
import { toDocument } from '@/components/ToMxamineState'
import craft from '@/views/engineering/treeBom/craft'
import { queryLog } from '@/api/engineering/deitalBom'

export default {
  name: 'CreateBom',
  components: {
    jcForm,
    search,
    numberCode: () => import('@/components/NumberCode'),
    craft
  },
  mixins: [getForm, searData],
  props: {
    FNUMBER: {
      type: String,
      required: true,
      default: ''
    },
    addBom: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      aotuTab: false,
      fnumber1: '',
      bomCode: '0',
      craftVis: false, // 工艺列表
      ftype: 0,
      fbillNo: 'fnumber', // 编码
      activeName: 'product', // 默认主产品 product Other
      tableHeight: '56vh',
      parentTableVisible: false, // 弹窗
      disabled: true, // 按钮禁用
      isTable: 0,
      flg: false, // true(警告提示后提交)/false(第一次提交)
      // 父分页
      parentPagination: {
        total: 0, // 总条目
        pageNum: 1, // 当前页
        size: 50 // 每页显示多少条数据
      },
      // 子分页
      sonPagination: {
        total: 0, // 总条目
        pageNum: 1, // 当前页
        size: 50 // 每页显示多少条数据
      },
      // 子项弹窗表格数据
      sonDialogTableData: [],
      // 父项数据
      parentTableData: [],
      // 父项表头
      parentTableHeader: [
        // 搜索编码，物料ID隐藏
        { label: this.$t('market.fmaterial'), prop: 'FNUMBER' },
        { label: this.$t('shop.fspecificaTion'), prop: 'FSPECIFICATION' },
        { label: this.$t('market.fmodel'), prop: 'FMODEL' },
        {
          label: this.$t('market.descrip'),
          prop: 'FDESCRIPTION',
          minWidth: '150px'
        },
        { label: this.$t('market.createDate'), prop: 'FCREATEDATE' }
      ],
      // 子项表头
      sonTableHeader: [],
      // 子项弹窗表格表头
      sonDialogHeaderTable: [
        {
          label: this.$t('shop.fname2'),
          prop: 'fname'
        },
        {
          label: this.$t('shop.fspecificaTion'),
          prop: 'fspecificaTion'
        },
        {
          label: this.$t('market.fdescripTion'),
          prop: 'fdescripTion',
          minWidth: '200px'
        },

        { label: this.$t('market.funit'), prop: 'funit', width: '80' },
        {
          label: this.$t('market.createDate'),
          prop: 'fcreateDate'
        },
        {
          label: this.$t('market.forbidStatu'),
          type: 'tag',
          prop: 'fforbidStatus'
        },
        {
          label: this.$t('market.documentstatus'),
          type: 'state',
          prop: 'fdocumentStatus'
        }
      ],
      // 子表数据
      sonTableData: [],
      prodValue: {}, // 产品表单值
      prodOptions: {}, // 产品表单元素
      // 子项类型
      materialTypes: [],
      // 发料方式
      issueMaterialsOptions: [],
      // 点击行的序号
      tableIndex: 0,
      serial: 1, // 序列号
      fnumber: '', // 父类编码
      FMATERIALCOST: 0, // 物料成本
      FMATERIALID: 0, // 父项物料ID
      tagNumber: '', // 搜索编码tag
      otherLogTableData: [], // 日志数据
      // 其它表头
      logTableHeader: [
        { label: this.$t('market.fdate'), prop: 'createDate' },
        { label: this.$t('market.operator'), prop: 'fname' },
        { label: this.$t('market.deaprt'), prop: 'fdeaprt' },
        { label: this.$t('market.ipSite'), prop: 'fip' },
        { label: this.$t('market.describe'), prop: 'fdescribe' }
      ],
      otherUrlObject: {}, // 其它审核人
      otherOptions: {}, // 其它
      // 日志分页
      otherPagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      scrollValue: 60, // 滚动条值
      width: 60,
      height: 60,
      FDOCUMENTSTATUS: '', // 状态
      srcList: [], // 图片预览
      expands: [] // 行唯一标识
    }
  },
  computed: {
    Summation() {
      const num = this.sonTableData.map(item => {
        const { FPRICE, FDOSAGE } = item
        item.money = FPRICE * FDOSAGE
        return item.money
      })
      const total = num.reduce((total, num) => {
        return total + num
      }, 0)
      return parseInt(total * 100) / 100
    },
    materialScience() {
      return this.Summation + Number(this.prodValue.FLABORCOST)
    }
  },
  watch: {
    Summation: {
      handler(val) {
        if (val === 0) {
          return
        }
        this.prodValue.FMATERIALCOST = val
      },
      immediate: true,
      deep: true
    },
    FNUMBER: {
      handler(val) {
        this.handleOther()
        this.getQueryBomchildList(val)
      },
      immediate: true
    },
    addBom: {
      handler(val) {
        this.parentSelectRow(val)
      },
      immediate: true,
      deep: true
    }
  },
  mounted() {
    // 详情页-新建BOM(复制)
    if (this.$route.query.val) {
      const { val } = this.$route.query
      this.prodValue = val
      this.FMATERIALID = val.FMATERIALID
      this.fnumber = val.FNUMBER
      this.parentSelectRow().then(() => {
        this.sonTableData = val.child
        this.sonTableData.map((item, index) => {
          item.FSEQ = index
        })
      })
    }
    // 生成BOM-
    if (this.$route.query.fnumberArr.length > 0) {
      this.$route.query.fnumberArr.forEach((item, index) => {
        this.newHang()
        this.sonSelectRow(item, index)
      })
    }
  },
  created() {
    this.handleSetForm()
    this.handleGetSubType()
    this.handleGetMaterials()
  },
  methods: {
    // 输入物料编码
    getAtuo(index, fnumber) {
      this.tableIndex = index
      this.aotuTab = true
      var vm = this
      clearTimeout(vm.timer) // 清除未执行的代码，重置回初始化状态
      vm.timer = setTimeout(() => {
        this.fnumber1 = fnumber
      }, 600)
    },
    // 放大图片
    enlargeImg(item) {
      this.width = item
      this.height = item
    },
    // 父表格搜索Tag
    handleParentTag(val) {
      this.$refs.inputValue.searData[0].val = ''
      this.tagNumber = val
      this.searCollData.fnumber = this.tagNumber
      this.getMaterielList()
    },
    // 子表格搜索Tag
    handleSonTag(val) {
      this.$refs.sonInputValue.searData[0].val = ''
      this.tagNumber = val
      this.searCollData.fnumber = this.tagNumber
      this.btnSearch()
    },
    // 子物料跳转详情
    handleSonDetailMater(FNUMBER) {
      this.$emit('jumpDetail', FNUMBER)
    },
    // 跳转物料详情
    handleDetailMater() {
      this.$emit('jumpDetail', this.prodValue.FNUMBER)
    },
    fun(index, value) {
      const currentLine = this.sonTableData[index]
      this.$set(currentLine, 'FMATERIALTYPE', value)
    },
    fun2(index, value) {
      const mode = this.sonTableData[index]
      this.$set(mode, 'FISSUETYPE', value)
    },
    // 清空弹窗数据
    closeFun() {
      this.tagNumber = ''
      this.parentTableVisible = false
      this.parentPagination.pageNum = 1
      this.sonPagination.pageNum = 1
      Object.keys(this.searCollData).forEach(key => {
        this.searCollData[key] = ''
      })
    },
    // 父项搜索
    queryParentSearch() {
      this.parentPagination.pageNum = 1
      this.getMaterielList()
    },
    // 子类搜索
    querySonSearch() {
      this.sonPagination.pageNum = 1
      this.btnSearch()
    },
    // 获取子项类型数据
    async handleGetSubType() {
      const { data: res } = await queryViewItem({
        viewName: 'SX_V_BOMCHILDTYPE'
      })
      this.materialTypes = res.list.map(item => {
        return { FMATERIALTYPE: item.fname, FVALUE: item.fid }
      })
    },
    // 查询发料方式列表
    async handleGetMaterials() {
      const { data: res } = await queryViewItem({
        viewName: 'SX_V_UNIT_ISSUETYPE'
      })
      this.issueMaterialsOptions = res.list.map(item => {
        return { FISSUETYPE: item.fname, FVALUE: item.fid }
      })
    },
    // 打开物料编码弹窗
    async getMaterielList() {
      this.isTable = 0
      const { pageNum, size } = this.parentPagination
      const DATA = {
        pageNum,
        pageSize: size,
        search: this.searCollData
      }
      const { data: RES, total, code, message } = await queryBomTreeDataList(
        DATA
      )
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      this.srcList = RES.map(item => item.FIMG)
      this.parentTableData = RES
      this.parentPagination.total = total
      this.parentTableVisible = true
    },
    // 获取数据
    async getQueryBomchildList(fnumber) {
      if (fnumber === '请添加数据' || !fnumber) {
        return
      }
      const { data: RES } = await queryBomchildList({ fnumber })
      const {
        FNUMBER,
        FMATERIALID,
        FID,
        FMATERIALCOST,
        child,
        fcreator, // 创建人
        fcreatedate, // 创建时间
        fmodifier, // 修改人
        fmodifydate, // 修改时间
        fapprover, // 审核人
        fapprovedate, // 审核时间
        fforbidder, // 禁用人
        fforbiddate // 禁用时间
      } = RES
      this.fnumber = FNUMBER
      this.FMATERIALID = FMATERIALID
      this.FID = FID
      this.FMATERIALCOST = FMATERIALCOST
      this.prodValue = RES
      const item = toDocument(RES)
      this.FDOCUMENTSTATUS = item.FDOCUMENTSTATUS
      this.srcList = child.map(item => item.FIMG)
      this.sonTableData = child.map(item => {
        item.FISSKIP = Boolean(item.FISSKIP)
        return item
      })
      // 其它
      this.otherUrlObject = {
        fcreator,
        fcreatedate,
        fmodifier,
        fmodifydate,
        fapprover,
        fapprovedate,
        fforbidder,
        fforbiddate
      }
    },
    // 查询子类弹窗编码
    async btnSearch() {
      const { pageNum, size } = this.sonPagination
      const postData = {
        pageNum,
        pageSize: size,
        search: this.searCollData
      }
      const { data: res, code, message } = await queryTBdMaterialList(postData)
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      const { array, total } = res
      this.srcList = array.map(item => item.FING)
      this.sonDialogTableData = array
      this.sonPagination.total = total
    },
    // 新增行
    newHang() {
      this.sonTableData.push({
        FMATERIALID: this.serial++, // 唯一的id
        FSEQ: this.serial++, // 序号
        FNUMBER: null, // 物料编码
        FDESCRIPTION: null, // 物料描述
        FSPECIFICATION: null, // 物料规格
        FUNITNAME: '', // 单位
        FMATERIALTYPE: '1', // 选中值
        FISSUETYPE: 42, // 选中值
        FPRICE: 0, // 单价
        FISSKIP: false,
        FDOSAGE: 0, // 用量
        money: 0, // 金额
        id: uuid.v1()
      })
    },
    // 单击行
    rowClick(row) {
      this.indexSelf = row.index
    },
    // 删除行
    delHang() {
      this.sonTableData.splice(this.indexSelf, 1)
    },
    // 新增子BOM
    async handleGetMaterialSon(row, index) {
      this.isTable = 1
      // if (!this.prodValue.FNUMBER.trim()) {
      //   this.disabled = true
      //   return
      // }
      this.tableIndex = index
      const { pageNum, size } = this.sonPagination
      const postData = { pageNum, pageSize: size, FNUMBER: this.FNUMBER }
      const { data: res, code, message } = await queryTBdMaterialList(postData)
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      const { array, total } = res
      this.sonDialogTableData = array.map(item => {
        return (toMxAmina(item), Disable(item))
      })
      this.sonPagination.total = total
      this.parentTableVisible = true
    },
    // 选中父项弹窗表格行
    async parentSelectRow(item) {
      const { FMATERIALID, FNUMBER } = item
      if (item) {
        this.FMATERIALID = FMATERIALID
        this.fnumber = FNUMBER
      }
      if (!FMATERIALID) return
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const { data: RES, code, message } = await queryMaterialfather({
        fmateriAalId: this.FMATERIALID
      })
      if (code !== 0) {
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      loading.close()
      this.prodValue = RES
      this.disabled = false
      this.parentTableVisible = false
      this.closeFun()
    },
    // 选中子项弹窗表格行
    async sonSelectRow(item, index) {
      const { fmaterialId: fmateriAalId, fnumber } = item
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      const { data: res, code, message } = await queryMaterialSon({
        fmateriAalId,
        fnumber
      })
      if (code !== 0) {
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
        return
      }
      loading.close()
      const {
        fname,
        FUNITNAME,
        FDOSAGE,
        FMATERIALID,
        FNUMBER,
        FPRICE,
        FDESCRIPTION,
        FISSKIP,
        FSPECIFICATION,
        FISSUETYPEID
      } = res
      const obj = {
        fname,
        FUNITNAME,
        FDOSAGE,
        FMATERIALID,
        FNUMBER,
        FPRICE,
        FDESCRIPTION,
        FISSKIP: FISSKIP === 1,
        FSPECIFICATION,
        FISSUETYPEID
      }
      for (const key in obj) {
        const index1 = this.tableIndex || index || 0
        this.sonTableData[index1][key] = obj[key]
      }
      this.parentTableVisible = false
      this.closeFun()
    },
    // 保存列表数据
    async preservation() {
      const fTreeEntity = this.sonTableData.map(item => {
        const {
          FUNITNAME,
          FISSUETYPE,
          FISSKIP,
          FMATERIALID,
          FMATERIALTYPE,
          FDOSAGE,
          FPRICE
        } = item
        return {
          FUNITNAME,
          FISSKIP: Number(FISSKIP),
          FISSUETYPE,
          FMATERIALID,
          FMATERIALTYPE,
          FDOSAGE,
          FPRICE
        }
      })
      const DATA = {
        flg: this.flg,
        FCREATEORGID: this.team,
        FUSEORGID: this.team,
        FLABORCOST: this.prodValue.FLABORCOST,
        FMATERIALID: this.FMATERIALID,
        workingHours: this.prodValue.workingHours,
        artificialSum: this.prodValue.artificialSum,
        fTreeEntity
      }
      // if (DATA.FLABORCOST === 0) {
      //   this.$message.warning('人工成本不能小于0,请重新输入!')
      //   return
      // }
      for (const item of DATA.fTreeEntity) {
        const ARRAY = Object.values(item)
        const RES = ARRAY.includes('' || undefined) || DATA.FMATERIALID === 0
        if (RES === true || RES.FDOSAGE === 0) {
          this.$message.warning('表格数据不能为空或用量不能为0!')
          return
        }
      }
      const { data: res, code, message } = await insertBom(DATA)
      if (code === 0) {
        this.$message({ showClose: true, message: message, type: 'success' })
        if (this.FNUMBER) { // 复制BOM
          this.$emit('replaceNode', res)
        } else { // 左侧添加bom后刷新
          this.$emit('addNode', res, 2)
        }
      } else if (code === 1) {
        this.$message.error(message)
      } else if (code === 2) {
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async() => {
            DATA.flg = true
            const { message, code } = await insertBom(DATA)
            if (code === 0) {
              this.$message({
                showClose: true,
                message: message,
                type: 'success'
              })
            } else if (code === 1) {
              this.$confirm(message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
          .catch(() => {
            this.$message.info('已取消保存')
          })
      }
    },
    // 打开工艺列表
    handleMagnifier(val) {
      if (val === 'fcraftName') {
        this.craftVis = true
      }
    },
    // 获取工艺数据
    craftData(item) {
      if (item.fid) {
        this.prodValue.fcraftName = item.fname
        this.prodValue.fcraftId = item.fid
        this.prodValue.FLABORCOST = item.totalSumPrice
      }
      this.craftVis = false
    },
    // 删除bom
    deleteBom(index) {
      if (index === 0) {
        this.$message.warning('不能删除首行数据')
        return
      }
      this.sonTableData.splice(index, 1)
      this.$message.success('删除行成功')
    },
    // 隐藏显示日志, 获取表单数据
    async handleOther() {
      const fbillNo = `BOM:${this.FNUMBER}`
      const { data: res, code, message } = await queryLog({
        ...this.otherPagination,
        fbillNo
      })
      if (code !== 0) {
        this.$message.warning(message)
        return
      }
      const { array, total } = res
      this.otherLogTableData = array
      this.otherPagination.total = total
    }
  }
}
</script>

<style scoped lang="scss">
.copyBom {
  @include bomCreate;
  min-width: 1550px;
  .tool {
    margin-bottom: 0;
  }
  padding: 0;
  .bom-form {
    position: relative;
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
.materielImg {
  width: 40px;
  height: 30px;
}
.avatar {
  width: 150px;
  height: 150px;
}
.el-form-item {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 0;
}
.form-layout {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 200px;
}
.tabPosit {
  padding: 5px;
  box-shadow: 0 0 2px #aaa;
  position: absolute;
  top: 3.6vh;
  left: 0;
  background-color: rgba(255, 255, 255, 1);
  z-index: 99999 !important;
}
.tabPosit1 {
  padding: 5px;
  box-shadow: 0 0 2px #aaa;
  position: absolute;
  top: -25.69vh;
  left: 0;
  background-color: rgba(255, 255, 255, 1);
  z-index: 9999;
}
</style>
